import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/assets/assets.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_widget.dart';
import 'package:pornhub_app/components/no_more/no_data.dart';
import 'package:pornhub_app/components/tab_bar_indicator/easy_fixed_indicator.dart';
import 'package:pornhub_app/components/text_view.dart';
import 'package:pornhub_app/model/engagement/engagement_model.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/utils/logger.dart';
import 'package:pornhub_app/views/engagement/controllers/engagement_search_result_page_controller.dart';

import 'engagement_list_cell.dart';
import 'lou_feng_cell.dart';

class EngagementSearchResultPage extends StatefulWidget {
  const EngagementSearchResultPage({super.key});

  @override
  State<StatefulWidget> createState() => _EngagementSearchResultPageState();
}

class _EngagementSearchResultPageState
    extends State<EngagementSearchResultPage> {
  final controller = Get.find<EngagementSearchResultPageController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _searchBar(),
      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return [
            SliverAppBar(
              pinned: true,
              title: Container(
                width: double.infinity,
                alignment: Alignment.topLeft,
                child: TabBar(
                  controller: controller.tabController,
                  isScrollable: false,
                  labelColor: COLOR.color_DD001B,
                  indicatorColor: COLOR.color_DD001B,
                  tabs: controller.tabs,
                  dividerHeight: 0,
                  indicator: EasyFixedIndicator(
                    color: COLOR.color_DD001B,
                    width: 10.w,
                    height: 3.w,
                  ),
                ),
              ),
              elevation: 0,
              titleSpacing: 0,
              leadingWidth: 0,
              backgroundColor: Colors.white,
              shadowColor: const Color.fromRGBO(0, 0, 0, 0),
              surfaceTintColor: Colors.white,
              leading: const SizedBox(),
            ),
            SliverToBoxAdapter(
              child: SizedBox(height: 12.h), // Spacer
            ),
          ];
        },
        body: TabBarView(
          controller: controller.tabController,
          children: [
            Obx(
              () => _buildBody(controller.youngModels.value, 0),
            ),
            Obx(
              () => _buildBody(controller.louFengs.value, 1),
            ),
          ],
        ),
      ),
    );
  }

  AppBar _searchBar() {
    return AppBar(
      shadowColor: const Color.fromRGBO(0, 0, 0, 0),
      surfaceTintColor: Colors.white,
      automaticallyImplyLeading: true,
      titleSpacing: 0,
      title: Flex(
        direction: Axis.horizontal,
        children: [
          Expanded(
            child: Container(
              width: double.infinity,
              height: 38.w,
              clipBehavior: Clip.hardEdge,
              alignment: Alignment.center,
              margin: EdgeInsets.only(right: 20.w),
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(16.w),
                color: COLOR.color_F5F5F5,
              ),
              child: Flex(
                direction: Axis.horizontal,
                children: [
                  Expanded(
                    child: TextField(
                      controller: controller.searchTextController,
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 13.w,
                      ),
                      decoration: InputDecoration(
                        isCollapsed: true,
                        hintText: "搜索你想要的服务（例如：口交）",
                        hintStyle: TextStyle(
                            color: COLOR.color_999999, fontSize: 13.w),
                        border: InputBorder.none,
                      ),
                    ),
                  ),
                  Image.asset(
                    Assets.iconSearch,
                    height: 18.w,
                    width: 18.w,
                  ),
                ],
              ),
            ),
          ),
          GestureDetector(
            onTap: () {
              controller.searchArgument.value =
                  controller.searchTextController.text;
              controller.loadHttpList(isRefresh: true);
            },
            child: Container(
              margin: EdgeInsets.only(right: 14.w),
              child: TextView(
                text: '搜索',
                color: COLOR.color_333333,
                fontSize: 16.w,
              ),
            ),
          ),
        ],
      ),
      backgroundColor: Colors.white,
    );
  }

  Widget _buildBody(List<EngagementModel> models, int type) {
    return BaseRefreshWidget(
      controller,
      child: Container(
        child: models.length > 0
            ? type == 0
                ? ListView.builder(
                    itemCount: models.length,
                    itemBuilder: (context, index) {
                      return EngagementListCell(
                        data: models[index],
                      ).onOpaqueTap(() {
                        Get.toEngagementDetail(
                            meetUserId: models[index].meetUserId ?? 0);
                      });
                    },
                  )
                : ListView.builder(
                    itemCount: models.length,
                    itemBuilder: (context, index) {
                      return LouFengCell(
                        data: models[index],
                      ).onOpaqueTap(() {
                        Get.toEngagementDetail(
                            meetUserId: models[index].meetUserId ?? 0);
                      });
                    },
                  )
            : NoData(),
      ),
    );
  }

  @override
  void dispose() {
    controller.searchTextController.dispose();
    super.dispose();
  }
}
